﻿Namespace MagicWindow

    Module mMagicWindow

        Private mVisible As Boolean = False

        Private WithEvents SkillsTreeWindow As GUI.MirWindow
        Private WithEvents SkillScroll As GUI.MirScrollBar
        Private WithEvents HideTree As GUI.MirButton
        Private SelectedSkillTreeTab As Integer = 0

        Private WithEvents MappingsWindow As GUI.MirWindow
        Public SelectedMappingsTab As Integer = 0

        Private lstSkills As New List(Of PlayerSkill)
        Private IconMap([Enum].GetNames(GetType(Protocol.Skills)).GetUpperBound(0)) As Point

        Private IconPages([Enum].GetNames(GetType(Protocol.Skills)).GetUpperBound(0)) As Integer
        Private IconIndex([Enum].GetNames(GetType(Protocol.Skills)).GetUpperBound(0)) As Integer


        Private MapingsMap([Enum].GetNames(GetType(Protocol.SkillKey)).GetUpperBound(0)) As Point
        Private HoverBorder As Color = Color.Gold
        Private HoverSkill As Skills
        Private SelectedBorder As Color = Color.Orange
        Public SelectedSkill As Skills
        Private isHovering As Boolean = False
        Public isSelected As Boolean = False

        Public Sub Init()

            SkillsTreeWindow = New GUI.MirWindow("SkillTree", 0, 0, 359, 448, True)

            SkillScroll = New GUI.MirScrollBar(SkillsTreeWindow, "SkillScroll", 339, 59, 12, 304, Libs.GameInter.Image(712).Header.Height - 336, EvoLib.Protocol.ImgLibs.GameInter, 1226)

            HideTree = New GUI.MirButton(SkillsTreeWindow, "HideTree", 317, 401, 0.0F, ImgLibs.GameInter, 0, 3036, 3036)

            MappingsWindow = New GUI.MirWindow("MappingsWindow", 359, 0, 220, 196, True)

            IconMap(Protocol.Skills.Fireball) = New Point(0, 0)
            IconMap(Protocol.Skills.Fencing) = New Point(0, 0)
            IconMap(Protocol.Skills.Slaying) = New Point(0, 1)
            IconMap(Protocol.Skills.Spiritsword) = New Point(4, 0)
            IconMap(Protocol.Skills.Healing) = New Point(0, 0)

            IconPages(Protocol.Skills.Fireball) = 0
            IconPages(Protocol.Skills.Fencing) = 7
            IconPages(Protocol.Skills.Slaying) = 7
            IconPages(Protocol.Skills.Spiritsword) = 7
            IconPages(Protocol.Skills.Healing) = 4

            IconIndex(Protocol.Skills.Fireball) = 1
            IconIndex(Protocol.Skills.Fencing) = 5
            IconIndex(Protocol.Skills.Slaying) = 13
            IconIndex(Protocol.Skills.Spiritsword) = 7
            IconIndex(Protocol.Skills.Healing) = 3

            MapingsMap(Protocol.SkillKey.F1) = New Point(0, 0)
            MapingsMap(Protocol.SkillKey.F2) = New Point(1, 0)
            MapingsMap(Protocol.SkillKey.F3) = New Point(2, 0)
            MapingsMap(Protocol.SkillKey.F4) = New Point(3, 0)
            MapingsMap(Protocol.SkillKey.F5) = New Point(0, 1)
            MapingsMap(Protocol.SkillKey.F6) = New Point(1, 1)
            MapingsMap(Protocol.SkillKey.F7) = New Point(2, 1)
            MapingsMap(Protocol.SkillKey.F8) = New Point(3, 1)
            MapingsMap(Protocol.SkillKey.F9) = New Point(0, 2)
            MapingsMap(Protocol.SkillKey.F10) = New Point(1, 2)
            MapingsMap(Protocol.SkillKey.F11) = New Point(2, 2)
            MapingsMap(Protocol.SkillKey.F12) = New Point(3, 2)

        End Sub

        Public Sub ClearSkillsList()
            lstSkills = New List(Of PlayerSkill)
        End Sub

        Public Sub AddSkill(desc As PlayerSkill)
            lstSkills.Add(desc)
        End Sub

        Public Sub UpdateSkill(desc As PlayerSkillDesc)
            If lstSkills.Count > 0 Then
                For i As Integer = 0 To lstSkills.Count - 1
                    If lstSkills(i).BYTE_SKILL = desc.BYTE_SKILL Then
                        Dim S As PlayerSkill = lstSkills(i)
                        S.LEVEL_INFO = desc
                        lstSkills.RemoveAt(i)
                        lstSkills.Add(S)
                        Exit Sub
                    End If
                Next
            End If

        End Sub

        Public Sub Shutdown()

            If mVisible Then ToggleSkillTree()

        End Sub

        Public Sub ToggleSkillTree()

            If mVisible Then

                Windows.RemoveWindow(SkillsTreeWindow)
                Windows.RemoveWindow(MappingsWindow)
                isSelected = False
                mVisible = False

            Else

                Windows.AddWindow(SkillsTreeWindow, Windows.WindowType.AlwaysOnTop)
                Windows.AddWindow(MappingsWindow, Windows.WindowType.AlwaysOnTop)
                mVisible = True

            End If

        End Sub

        Private Sub DrawTree(mode As Globals.DrawingMode)

            Dim yoff As Integer = 0 - SkillScroll.Value

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GameInter.Image(712 + SelectedSkillTreeTab)
                    .Draw(0, 0 - SkillScroll.Value, 0.0F)
                End With

                For Each desc As PlayerSkill In lstSkills
                    If IconPages(desc.BYTE_SKILL) = SelectedSkillTreeTab Then
                        DrawSkillBox(desc.BYTE_SKILL)
                        If desc.KEY_MAP.BYTE_KEY <> SkillKey.NotSet Then
                            DrawSkillKey(desc.BYTE_SKILL, desc.KEY_MAP.BYTE_KEY)
                        End If
                    End If
                Next

            ElseIf mode = Globals.DrawingMode.Text Then
                For Each desc As PlayerSkill In lstSkills
                    If IconPages(desc.BYTE_SKILL) = SelectedSkillTreeTab Then
                        DrawSkillLevel(desc.BYTE_SKILL, desc.LEVEL_INFO.BYTE_LEVEL)
                    End If
                Next

            ElseIf mode = Globals.DrawingMode.Line Then

                If isSelected Then
                    If IconPages(SelectedSkill) = SelectedSkillTreeTab Then
                        DrawSkillHiLite(SelectedSkill, Color.Gold)
                    End If
                End If
                If isHovering Then
                    If IconPages(HoverSkill) = SelectedSkillTreeTab Then
                        DrawSkillHiLite(HoverSkill, Color.Yellow)
                    End If
                End If

            End If

        End Sub

        Private Sub DrawSkillBox(BYTE_SKILL As Skills)
            Dim yoff As Integer = 0 - SkillScroll.Value
            Libs.MIcon.Image(IconIndex(BYTE_SKILL)).Draw(11 + (IconMap(BYTE_SKILL).X * 60), 16 + (IconMap(BYTE_SKILL).Y * 60) + yoff, 0.0F)
        End Sub

        Private Sub DrawSkillKey(BYTE_SKILL As Skills, key As SkillKey)

            Dim yoff As Integer = 0 - SkillScroll.Value
            With Libs.GameInter1.Image(31 + key)
                .Draw(11 + (IconMap(BYTE_SKILL).X * 60) + 32 - .Header.Width, 16 + (IconMap(BYTE_SKILL).Y * 60) + yoff + 32 - .Header.Height, 0.0F)
            End With

        End Sub

        Private Sub DrawSkillHiLite(BYTE_SKILL As Skills, col As Color)
            Dim yoff As Integer = 0 - SkillScroll.Value
            Engine.DrawBox(11 + (IconMap(BYTE_SKILL).X * 60), 11 + (IconMap(BYTE_SKILL).X * 60) + 32, 16 + (IconMap(BYTE_SKILL).Y * 60) + yoff, 16 + (IconMap(BYTE_SKILL).Y * 60) + yoff + 32, 0.0F, 1, col)
        End Sub


        Private Sub DrawSkillLevel(BYTE_SKILL As Skills, BYTE_LEVEL As Integer)
            Dim yoff As Integer = 0 - SkillScroll.Value
            DirectX.ProFont9.DrawNormalText(BYTE_LEVEL, 49 + (IconMap(BYTE_SKILL).X * 60), 50 + (IconMap(BYTE_SKILL).Y * 60) + yoff, Color.White, 0.0F)
        End Sub

        Private Sub SkillsTreeWindow_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles SkillsTreeWindow.DrawWindow

            If mode = Globals.DrawingMode.Overlay Then

                DirectX.D3D.SetRenderTarget(0, DirectX.SkillSurface)
                'Begin D3D scene
                DirectX.D3D.BeginScene()
                'Clear target and z-buffer
                DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Microsoft.DirectX.Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0)


                'Begin GameSprite rendering
                DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)
                'Set draw mode to plain
                Engine.SetDrawModeNormal(1.0, Color.White)

                'Draw the tree... in normal mode
                DrawTree(Globals.DrawingMode.Normal)

                DirectX.GameSprite.End()
                DirectX.D3D.EndScene()

                Engine.ResetShader()

                Engine.SetDrawModeNormal(1.0F, Color.White)

                DrawTree(Globals.DrawingMode.Text)

                DirectX.D3D.BeginScene()

                Engine.UseLineShader()
                DirectX.D3D.VertexFormat = DirectX.FVF_LINEVERT

                DrawTree(Globals.DrawingMode.Line)

                DirectX.D3D.EndScene()


            ElseIf mode = Globals.DrawingMode.Normal Then

                Libs.GameInter.Image(711).Draw(x - 76, y - 32, z)

                For i As Integer = 0 To 7

                    If i = SelectedSkillTreeTab Then
                        Libs.GameInter.Image(722 + (i * 2)).Draw(x + 46 + (i * 33), y + 9, z)
                    Else
                        Libs.GameInter.Image(721 + (i * 2)).Draw(x + 46 + (i * 33), y + 9, z)
                    End If

                Next

                DirectX.BlitD3D(DirectX.SkillTexture, x + 19, y + 42, 304, 336, z)

            ElseIf mode = Globals.DrawingMode.Text Then

                If isSelected Then
                    If IconPages(SelectedSkill) = SelectedSkillTreeTab Then
                        For Each playerskill As PlayerSkill In lstSkills

                            If playerskill.BYTE_SKILL = SelectedSkill Then

                                If playerskill.LEVEL_INFO.INT_LEVEL_EXP = -1 Then
                                    DirectX.ProFont9.DrawBorderText(SelectedSkill.ToString & " - Level " & playerskill.LEVEL_INFO.BYTE_LEVEL & vbCrLf & "Quest Required For Next Level", x + 25, y + 400, Color.White, Color.Black, z)
                                Else
                                    DirectX.ProFont9.DrawBorderText(SelectedSkill.ToString & " - Level " & playerskill.LEVEL_INFO.BYTE_LEVEL & vbCrLf & "Exp " & playerskill.LEVEL_INFO.INT_EXP & "/" & playerskill.LEVEL_INFO.INT_LEVEL_EXP, x + 25, y + 400, Color.White, Color.Black, z)
                                End If

                                Exit Sub

                            End If

                        Next
                    End If
                End If

            End If

        End Sub

        Private Sub SkillsTreeWindow_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles SkillsTreeWindow.MouseEvent

            If type = Mouse.MouseInputType.MouseClick Then

                If e.X >= 46 And e.X < 46 + (33 * 8) Then

                    If e.Y >= 9 And e.Y <= 9 + Libs.GameInter.Image(721).Header.Height Then

                        Dim tmpX As Integer = (e.X - 46)
                        SelectedSkillTreeTab = Math.DivRem(tmpX, 33, Nothing)

                        SkillScroll.Maximum = (Libs.GameInter.Image(712 + SelectedSkillTreeTab).Header.Height - 336)
                        SkillScroll.SetValue(0)

                    End If

                End If

                If isHovering Then
                    SelectedSkill = HoverSkill
                    isSelected = True
                Else
                    isSelected = False
                End If

            ElseIf type = Mouse.MouseInputType.MouseMove Then

                Dim x As Integer, y As Integer
                Dim pos As Point
                Dim yoff As Integer = 0 - SkillScroll.Value

                For Each skill As Skills In [Enum].GetValues(GetType(Skills))

                    If IconPages(skill) = SelectedSkillTreeTab Then
                        pos = IconMap(skill)
                        x = 30 + (pos.X * 60)
                        y = 58 + (pos.Y * 60) + yoff

                        If e.X >= x And e.X <= x + 32 Then
                            If e.Y >= y And e.Y <= y + 32 Then

                                HoverSkill = skill
                                isHovering = True
                                SetSkillTooltip(skill)
                                GoTo foundhover

                            End If
                        End If

                    End If

                Next

                GameWindow.HideTooltip()
                isHovering = False

foundhover:

            End If

        End Sub

        Private Sub SetSkillTooltip(skill As Skills)

            For Each playerskill As PlayerSkill In lstSkills

                If playerskill.BYTE_SKILL = skill Then

                    Dim txt As String = "\b\2" & skill.ToString & "\n\5\n\5Current Level: " & playerskill.LEVEL_INFO.BYTE_LEVEL & "\b" & _
                                                                    "\n\3lvl 1 - lv \b" & playerskill.LVL_1.BYTE_LEVEL & "\b - " & playerskill.LVL_1.INT_EXP & "xp" & _
                                                                    "\n\3lvl 2 - lv \b" & playerskill.LVL_2.BYTE_LEVEL & "\b - " & playerskill.LVL_2.INT_EXP & "xp" & _
                                                                    "\n\3lvl 3 - lv \b" & playerskill.LVL_3.BYTE_LEVEL & "\b - " & playerskill.LVL_3.INT_EXP & "xp" & _
                                                                    "\n\3lvl 4 - lv \b" & playerskill.LVL_4.BYTE_LEVEL & "\b - " & playerskill.LVL_4.INT_EXP & "xp" & _
                                                                     "\n\3lvl 5 - lv \b" & playerskill.LVL_5.BYTE_LEVEL & "\b - " & playerskill.LVL_5.INT_EXP & "xp" & _
                                                                    "\n\0" & playerskill.STR_DESC

                    txt = Replace(txt, "-1xp", "Quest")

                    GameWindow.SetTooltip(txt, 0, True)

                    Exit Sub
                End If

            Next

        End Sub

        Private Sub HideTree_Click() Handles HideTree.Click

            ToggleSkillTree()

        End Sub

        Private Sub MappingsWindow_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles MappingsWindow.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GameInter1
                    With .Image(21)
                        .Draw(x - 18, y - 30, z)
                    End With

                    .Image(22 + SelectedMappingsTab).Draw(x + 43 + (34 * SelectedMappingsTab), y + 10, z)

                End With

                For Each skill As PlayerSkill In lstSkills

                    If skill.KEY_MAP.BYTE_SET = SelectedMappingsTab Then

                        Libs.MIcon.Image(IconIndex(skill.BYTE_SKILL)).Draw(x + 19 + (MapingsMap(skill.KEY_MAP.BYTE_KEY).X * 49), y + 51 + (MapingsMap(skill.KEY_MAP.BYTE_KEY).Y * 48), z)

                    End If

                Next

            End If

        End Sub

        Private Sub MappingsWindow_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles MappingsWindow.MouseEvent

            If type = Mouse.MouseInputType.MouseClick Then

                If e.X >= 43 And e.X < (43 + (33 * 4)) Then
                    If e.Y >= 10 And e.Y < 46 Then

                        Dim tX As Integer = e.X - 43
                        SelectedMappingsTab = Math.DivRem(tX, 33, Nothing)

                    End If
                End If

            End If

        End Sub

        Public Sub CastSpell(key As SkillKey)

            For Each skill As PlayerSkill In lstSkills

                If skill.KEY_MAP.BYTE_KEY = key And skill.KEY_MAP.BYTE_SET = SelectedMappingsTab Then

                    Select Case skill.BYTE_SKILL

                        Case Skills.Fireball
                            Player.Cast1(Magics.FireballCast)

                        Case Skills.Healing
                            Player.Cast2(Magics.HealingCast)

                    End Select

                    Exit Sub
                End If

            Next

        End Sub

    End Module

End Namespace